package com.ihome.android.market2.asynctask;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;

import com.ihome.android.EAApp;
import com.ihome.android.analytic.JsonAnalytic;
import com.ihome.android.http.HttpActions;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.academy.R;
import com.ihome.android.market2.frag.AlbumListFragment;
import com.ihome.android.market2.frag.SpecialTopicFragment;
import com.ihome.android.market2.info.TemplateContentInfo;
import com.ihome.android.market2.view.DialogFactory;
import com.ihome.android.market2.view.DialogFactory.onBtnClickListener;
import com.ihome.android.market2.view.MyProgressDialog;
import com.ihome.android.util.exception.ITVException;
import com.ihome.android.util.log.LogUtil;

/**
 * 获取应用专辑信息
 * 
 * @author GaoYa
 * 
 */
public class GetAlbumListTask extends AsyncTask<Bundle, Integer, Bundle> {
	private final String					TAG				= GetAlbumListTask.class.getSimpleName();
	private Context							context;
	private MyProgressDialog				progressDialog;
	private int								contentID		= 0;
	private List<TemplateContentInfo>		mSectionContentList;
	private Fragment			fragment;

	public GetAlbumListTask(Context context,Fragment fragment) {
		this.context = context;
		this.fragment = fragment;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (!(context instanceof Activity))
			return;
		progressDialog = MyProgressDialog.getDialog(context, context.getString(R.string.user_grabing_data));
		progressDialog.show();
		progressDialog.setCancelable(true);
		progressDialog.setOnDismissListener(new OnDismissListener() {

			@Override
			public void onDismiss(DialogInterface dialog) {
				if (GetAlbumListTask.this.getStatus().compareTo(AsyncTask.Status.FINISHED) != 0)
					GetAlbumListTask.this.cancel(true);
			}
		});
	}

	@Override
	protected Bundle doInBackground(Bundle... params) {
		Bundle b = params[0];
		int sectionId = b.getInt(HttpKeys.KEY_SECTIONID);
		LogUtil.v(TAG, "sectionId = " + sectionId + ";");

		try {
			String sectionContent = new HttpActions(context).getSectionContent(EAApp.getApp().cacheKey, sectionId);
			LogUtil.d(TAG, "GetAlbumListTask--------->sectionContent = \n" + sectionContent);
			if (!TextUtils.isEmpty(sectionContent)) {
				mSectionContentList = JsonAnalytic.getInstance().anslyseTemplateContent(sectionContent);
			}
			else {
				cancel(true);
			}

		} catch (ITVException e) {
			e.printStackTrace();
			LogUtil.e(TAG, e.toString());
			b.putInt(HttpKeys.KEY_CODE, e.getStatusCode());
			b.putString(HttpKeys.KEY_DESCRIPTION, e.getMessage());
		}
		return b;
	}

	@Override
	protected void onPostExecute(Bundle result) {
		super.onPostExecute(result);
		if (progressDialog != null)
			progressDialog.dismiss();

		/** 请求出错处理 */
		if (result.containsKey(HttpKeys.KEY_CODE)) {
			if(ITVException.CODE_LOGINOUT_THREE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGINOUT_ONE == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_SESSIONKEY == result.getInt(HttpKeys.KEY_CODE)||ITVException.CODE_LOGIN_ONE == result.getInt(HttpKeys.KEY_CODE)){
				new DialogFactory(context).showDialog("",result.getString(HttpKeys.KEY_DESCRIPTION) + "(" + result.getInt(HttpKeys.KEY_CODE) + ")",context.getString(R.string.dialog_ok),new onBtnClickListener() {
					
					@Override
					public void btnRightClickListener(View v) {
						// TODO Auto-generated method stub
					}
					
					@Override
					public void btnNeutralClickListener(View v) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void btnLeftClickListener(View v) {
						// TODO Auto-generated method stub
						EAApp.getApp().exitApp(true);
					}
					
					@Override
					public void btnCloseClickListener(View v) {
						// TODO Auto-generated method stub
						
					}
				});
				
				return;
			}
			new DialogFactory(context).showDialog(result.getString(HttpKeys.KEY_DESCRIPTION) + "(" + result.getInt(HttpKeys.KEY_CODE) + ")");
			return;
		}
		
		if (mSectionContentList != null && fragment instanceof AlbumListFragment) {
			((AlbumListFragment) fragment).getAppSuccess(mSectionContentList, mSectionContentList.size());
		}
		
		if (mSectionContentList != null && fragment instanceof SpecialTopicFragment) {
			((SpecialTopicFragment) fragment).getAppSuccess(mSectionContentList, mSectionContentList.size());
		}
	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
		LogUtil.i(TAG, "--> onCancelled");
		if (progressDialog != null)
			progressDialog.dismiss();

	}

}
